﻿// --------------------------------------------------------------------------------------
// <copyright file="UniqueStringGenerator.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2013.12.24">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.Core.Utilities
{
    using System.Text;

    public static class UniqueStringGenerator
    {
        private static readonly string EnglishSigns = "aAeEoOuUiIdDyY-";

        private static readonly string[] VietnameseSigns = new string[]
        {
            "áàạảãâấầậẩẫăắằặẳẵ",

            "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",

            "éèẹẻẽêếềệểễ",

            "ÉÈẸẺẼÊẾỀỆỂỄ",

            "óòọỏõôốồộổỗơớờợởỡ",

            "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",

            "úùụủũưứừựửữ",

            "ÚÙỤỦŨƯỨỪỰỬỮ",

            "íìịỉĩ",

            "ÍÌỊỈĨ",

            "đ",

            "Đ",

            "ýỳỵỷỹ",

            "ÝỲỴỶỸ",
            " "
        };

        private static string RemoveSign4VietnameseString(string str)
        {
            StringBuilder sb = new StringBuilder();
            char[] inputCharArray = str.ToCharArray();

            foreach (char ch in inputCharArray)
            {
                for (int i = 0; i < VietnameseSigns.Length; i++)
                {
                    if (!VietnameseSigns[i].Contains(ch.ToString()))
                    {
                        // Travel up to the last element but not found relevant unicode char to replace.
                        // Conclude: current char is not unicode.
                        if (i == VietnameseSigns.Length - 1)
                        {
                            sb.Append(ch);
                        }
                    }
                    else
                    {
                        sb.Append(EnglishSigns[i]);
                        break;
                    }
                }
            }

            if (sb.Length > 0)
            {
                return sb.ToString();
            }

            return str;
        }

        public static string ParseToUniqueString(string source)
        {
            return RemoveSign4VietnameseString(source);
        }
    }
}
